﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quanlytruongtruongtieuhoc
{
    public partial class frmPhanconggiaovien : Form
    {
        public static int hien=0;
        public static string malop, mamon;
        public frmPhanconggiaovien()
        {
            InitializeComponent();
        }

       public static void frmPhanconggiaovien_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

       private void frmPhanconggiaovien_Load(object sender, EventArgs e)
       {
           try
           {
               clsKetnoi.taoKetnoi();
               SqlDataAdapter ap = new SqlDataAdapter("select distinct ma_mon,ten_mon from mon_hoc where ma_lv!='CN'", clsKetnoi.con);
               DataTable lop = new DataTable();
               lop.Clear();
               ap.Fill(lop);
               cbMalop.DataSource = lop;
               cbMalop.DisplayMember = "ten_mon";
               cbMalop.ValueMember="ma_mon";
               btCapnhat.Enabled = false;
               button1.Enabled = false;
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void frmPhanconggiaovien_FormClosing_1(object sender, FormClosingEventArgs e)
       {
           hien = 0;
       }
       private void loadData(DataGridView a,string ma)
       {
           try
           {
               if (ma == "System.Data.DataRowView")
               { }
               else
               {
                   a.Rows.Clear();
                   string nam = frmPhanconggvchunhiem.layNam();
                   SqlDataAdapter ap = new SqlDataAdapter("select distinct ma_khoi+'.'+ma_lop from lop_hoc_sinh where nam_hoc='"+nam+"' and ma_khoi+'.'+ma_lop not in(select ma_khoi+'.'+ma_lop from giang_day where ma_mon='"+ma+"' and nam_hoc='"+nam+"')", clsKetnoi.con);
                   DataTable monhoc = new DataTable();
                   monhoc.Clear();
                   ap.Fill(monhoc);
                   for (int i = 0; i < monhoc.Rows.Count; i++)
                   {
                       DataRow row = monhoc.Rows[i];
                       a.Rows.Add();
                       a.Rows[i].Cells[0].Value = i + 1;
                       a.Rows[i].Cells[1].Value = row[0].ToString();
                       SqlDataAdapter ap1 = new SqlDataAdapter("select ma_gv,ho+' '+ten as ten from giao_vien a,mon_hoc b where a.ma_lv=b.ma_lv and a.xoa='False' and b.ma_mon='" + ma+ "'", clsKetnoi.con);
                       DataTable gv = new DataTable();
                       gv.Clear();
                       ap1.Fill(gv);
                       (a.Columns[2] as DataGridViewComboBoxColumn).DataSource = gv;
                       (a.Columns[2] as DataGridViewComboBoxColumn).DisplayMember = "ten";
                       (a.Columns[2] as DataGridViewComboBoxColumn).ValueMember = "ma_gv";
                   }
                   for (int i = 0; i < dtPhancong.Rows.Count; i++)
                   {
                       DataGridViewRow r = dtPhancong.Rows[i];
                       r.Height = 28;
                   }

               }
           }
           catch (Exception ex)
           {
              MessageBox.Show(ex.Message);
           }
       }
        private void loadData1(DataGridView a,string ma)
        {
            try
            {
                if (ma == "System.Data.DataRowView")
                { }
                else
                {
                    a.Rows.Clear();
                    string nam = frmPhanconggvchunhiem.layNam();
                    SqlDataAdapter ap = new SqlDataAdapter("select distinct ma_khoi+'.'+ma_lop,a.ma_mon,c.ten_mon,b.ma_gv,ho+' '+ten from giang_day a,giao_vien b,mon_hoc c where a.ma_mon=c.ma_mon and a.nam_hoc='"+nam+"' and a.ma_mon='"+ma+"' and a.ma_gv=b.ma_gv", clsKetnoi.con);
                    DataTable monhoc = new DataTable();
                    monhoc.Clear();
                    ap.Fill(monhoc);
                    for (int i = 0; i < monhoc.Rows.Count; i++)
                    {
                        DataRow row = monhoc.Rows[i];
                        a.Rows.Add();
                        a.Rows[i].Cells[0].Value = i + 1;
                        a.Rows[i].Cells[1].Value = row[0].ToString();
                        a.Rows[i].Cells[2].Value=row[1].ToString();
                        a.Rows[i].Cells[3].Value=row[2].ToString();
                        a.Rows[i].Cells[4].Value = row[3].ToString();
                        a.Rows[i].Cells[5].Value=row[4].ToString();
                    }
                    for (int i = 0; i < a.Rows.Count; i++)
                    {
                        DataGridViewRow r = a.Rows[i];
                        r.Height = 28;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       private void cbMalop_SelectedIndexChanged(object sender, EventArgs e)
       {
           try
           {
               loadData(dtPhancong, cbMalop.SelectedValue.ToString());
               loadData1(dtXem, cbMalop.SelectedValue.ToString());
               btCapnhat.Enabled = true;
               button1.Enabled = true;
           }
           catch (Exception)
           { }
       }

       private void button3_Click(object sender, EventArgs e)
       {
           try
           {
               if (dtPhancong.Rows.Count > 0)
               {
                    DialogResult kq = new DialogResult();
                kq = MessageBox.Show("Có chắc lưu không ? ", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (DialogResult.OK == kq)
                {
                    string makhoi, malop, magv, mamon, nam;
                    nam = frmPhanconggvchunhiem.layNam();
                    mamon = cbMalop.SelectedValue.ToString();
                    for (int i = 0; i < dtPhancong.Rows.Count; i++)
                    {
                        DataGridViewRow row1 = dtPhancong.Rows[i];
                        try
                        {
                            magv = (((DataGridViewComboBoxCell)row1.Cells[2]).Value.ToString());
                            string malop1 = dtPhancong.Rows[i].Cells[1].Value.ToString();
                            makhoi = malop1.Substring(0, 1);
                            malop = malop1.Substring(2, 1);
                            SqlCommand com = new SqlCommand("insert into giang_day values('" + nam + "','" + mamon + "','" + makhoi + "','" + malop + "','" + magv + "')", clsKetnoi.con);
                            com.ExecuteNonQuery();
                            if (i == (dtPhancong.Rows.Count - 1))
                            {
                                MessageBox.Show("Đã lưu thành công. ", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Chưa chọn giáo viên.", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                            break;
                        }
                    }
                }
                   loadData(dtPhancong, cbMalop.SelectedValue.ToString());
                   loadData1(dtXem, cbMalop.SelectedValue.ToString());
               }
               else
               {
                   MessageBox.Show("Không còn lớp để xếp. ", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Error);
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               int a = dtXem.CurrentRow.Index;
               string magv = dtXem.Rows[a].Cells[4].Value.ToString();
               string lop = dtXem.Rows[a].Cells[1].Value.ToString();
               string nam = frmPhanconggvchunhiem.layNam();
               SqlCommand com = new SqlCommand("delete from giang_day where ma_gv='" + magv + "' and nam_hoc='" + nam + "' and ma_khoi='" + lop.Substring(0, 1) + "' and ma_lop='" + lop.Substring(2, 1) + "'", clsKetnoi.con);
               DialogResult kq = new DialogResult();
               kq = MessageBox.Show("Có chắc xóa không?", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
               if (DialogResult.OK == kq)
               {
                   com.ExecuteNonQuery();
                   MessageBox.Show("Đã xóa thành công.", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                   loadData1(dtXem, cbMalop.SelectedValue.ToString());
                   loadData(dtPhancong, cbMalop.SelectedValue.ToString());
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void btCapnhat_Click(object sender, EventArgs e)
       {
           int a = dtXem.CurrentRow.Index;
           malop = dtXem.Rows[a].Cells[1].Value.ToString();
           mamon = dtXem.Rows[a].Cells[2].Value.ToString();
           frmCapnhatphanconggd A = new frmCapnhatphanconggd();
           A.ShowDialog();
           loadData(dtPhancong, cbMalop.SelectedValue.ToString());
           loadData1(dtXem, cbMalop.SelectedValue.ToString());
       }
    }
}
